package com.masterclient.app.businessmodule.usermodule.userinfo.contract;

import com.eoner.common.bean.base.CommonBaseBean;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.address.AddressListBean;
import com.eoner.baselibrary.bean.personal.MobileChangeStateBean;
import com.eoner.baselibrary.bean.personal.UploadImgBean;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/08/02
*/
public class UserInfoPresenter extends UserInfoContract.Presenter{


    @Override
    public void uploadImage(File image, String type) {


        Map<String, RequestBody> map = new HashMap<>();
        RequestBody requestBody1 =  RequestBody.create(MediaType.parse("multipart/form-data"), image);

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        RequestBody imageBody = RequestBody.create(MediaType.parse("image/png"), image);
        builder.addFormDataPart("imgfile", "123.jpeg", imageBody);//imgfile 后台接收图片流的参数名
        builder.addFormDataPart("type",type);

        Observable<UploadImgBean> observable = RetrofitUtil.getInstance().uploadImage(builder.build().parts());
        Disposable disposable = toSubscribe(observable, new Consumer<UploadImgBean>() {
            @Override
            public void accept(@NonNull UploadImgBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.onUploadSuccess(bean.getData());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void updateUserInfo(Map<String,String> map) {
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().updateUserInfo(map);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.onUpdateHeadImgSucccess();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void getRegionList(String region_id) {
        Observable<AddressListBean> observable = RetrofitUtil.getInstance().getRegionList(region_id);
        Disposable disposable = toSubscribe(observable, new Consumer<AddressListBean>() {
            @Override
            public void accept(@NonNull AddressListBean bean) throws Exception {
                if(bean!=null&&bean.getData()!=null){
                    mView.getAddressList(bean.getData());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void mobile_change_state(String mobile) {
        Observable<MobileChangeStateBean> observable = RetrofitUtil.getInstance().mobile_change_state(mobile);
        Disposable disposable = toSubscribe(observable, new Consumer<MobileChangeStateBean>() {
            @Override
            public void accept(@NonNull MobileChangeStateBean bean) throws Exception {
                String code = "404";
                String msg = "数据异常";
                if(bean != null){
                    if(bean.getData() != null){
                        if( "0".equals(bean.getCode())){
                            msg = bean.getData().getSh_change_state_message();
                            code = bean.getData().getSh_change_state();
                        }else{
                            code = bean.getCode();
                            msg = bean.getMsg();
                        }
                    }
                }
                mView.mobile_change_state_result(code,msg);
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}